package main.java.com.lee.syn;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2021/12/17
 */
public class SelfAddNotSafe {
	public static void main(String[] args) throws InterruptedException {
		ExecutorService threadPool = Executors.newFixedThreadPool(3);
		CountDownLatch countDownLatch = new CountDownLatch(100);
		Demo demo = new Demo();
		for (int i=0; i<100; i++){
			threadPool.submit(() -> {
				for(int j=0;j<3;j++){
//					demo.add();
					demo.synchronizedAdd();
				}
				countDownLatch.countDown();
			});
		}
		countDownLatch.await();
		threadPool.shutdown();

		demo.print();

	}


}
class Demo{
	private int i = 0;

	public void add() {
		 i++;
	}

	public synchronized void synchronizedAdd() {
		i++;
	}

	public  void synchronizedBlockAdd() {
		synchronized (this) {
			i++;
		}
		// 其他操作
	}
	public void print() {
		System.out.println(i);
	}
}
